﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LeadTeam.Exam.Entities;
using LeadTeam.Exam.AdminTool.Forms;

namespace LeadTeam.Exam.AdminTool
{
    public partial class AddOrganizations : Form,ILookupOrganizations
    {
        //true parent and false child
        private bool flag = true;
        private AddOrganizationsController _controllor;
        private IOrganizationsView iOrgView;
        public AddOrganizationsController Controllor
        {
            get { return _controllor; }
            set
            {
                this._controllor = value;
            }
        }

        public AddOrganizations(IOrganizationsView iOrgView,bool flag)
        {
            InitializeComponent();
            this.iOrgView = iOrgView;
            this.flag = flag;
            if (flag)
            {
                this.lblOrg.Text = "父级组织";
            }
            else
            {
                this.lblOrg.Text = "子级组织";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (flag)
            {
                _controllor.model = new Organizations();
                _controllor.model.O_CREATE = DateTime.Now;
                _controllor.model.O_DESC = tbxDesc.Text.Trim();
                _controllor.model.O_PARENT_ID = Convert.ToInt32(tbxRelated.Tag);
                _controllor.model.O_NAME = tbxName.Text.Trim();

                //state request
                _controllor.SetRequestState();
                _controllor.AddOrganizationsEntityChild();

                this.Close();

                iOrgView.Controllor.UpdateModel();
                //state done
                _controllor.SetDoneState();
            }
            else 
            {
                _controllor.model = new Organizations();
                _controllor.model.O_CREATE = DateTime.Now;
                _controllor.model.O_DESC = tbxDesc.Text.Trim();
                _controllor.model.O_PARENT_ID = -1;
                _controllor.model.O_NAME = tbxName.Text.Trim();

                //state request
                _controllor.SetRequestState();
                _controllor.AddOrganizationsEntityParent(tbxRelated.Tag.ToString());

                this.Close();

                iOrgView.Controllor.UpdateModel();
                //state done
                _controllor.SetDoneState();
            }
        }

        //look up click
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            LookupOrganizationsController lookupOrgController = new LookupOrganizationsController
                (new LookupOrganizations(this),_controllor.MainFormHandle);
            lookupOrgController.view.ShowDialog();
        }

        public void SetOrganizations(int value, string name)
        {
            tbxRelated.Tag = value;
            tbxRelated.Text = name;
        }
    }
}
